layui.use(['laytpl','layer','form','util'], function(){
	var laytpl = layui.laytpl
	, layer = layui.layer
	, form = layui.form
	, $ = layui.$
	, util = layui.util;
	  function checkEmail(email) {

			// 对电子邮件的验证

			var myreg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;

			if (!myreg.test(email)) {

			return false;

			} else {

			return true;

			}
		  }
	//接收上一个页面的传值
	function getQueryVariable(variable){
	    var query = window.location.search.substring(1);
	    var vars = query.split("&");
	    for (var i=0;i<vars.length;i++) {
	        var pair = vars[i].split("=");
	        if(pair[0] == variable){return pair[1];}
	    }
	    return(false);
	} 
	 //监听提交
	 form.on('submit(formDemo)', function (data) {
			var data = $('#loginData').serialize()
 			console.log(data)
	       //登录
		var acount = $('#acount').val();
		var password = $("#pwd").val();
		var data ={
				'acount': acount,
				'password': password
		}
		console.log(data)
		 $.ajax({
	        type: "POST",
	        contentType:"application/json;charset=utf-8",
	        url: "http://127.0.0.1:8080/blog/user/login",
	        data:JSON.stringify(data),
	        success: function (res) {
	        	var rs = JSON.parse(res);
	        	if(rs.code == 200){
	        		layer.msg("登录成功！")
	        		layui.sessionData('user',{
	        			key: 'data',
	        			value: rs.data.data
	        		})
	        		console.log(layui.sessionData('user'))
	        		window.location.href="/blog/blogg/index.html"
	        	}else{
	        		layer.msg(rs.msg)
	        	}
	        },complete:function(res){
	            console.log(res.msg);
	        },
	        error:function(res){
	        	console.log(res.msg);
	        	layer.msg(rs.msg)
	        }
   		 });
	    return false;
	    });
	//发送验证码
	$('#getVeriCode').on('click',function(){
		var email =  $('#email').val()
		,name = $('#name').val()
		,password = $('#pwd').val()
		if($.isEmptyObject(email)){
			layer.msg('请先填写邮箱！')
			return false;
		}
	   	 if(!checkEmail(email)){
			 layer.msg('邮箱格式有误！')
			 return false;
		 }
		$.ajax({
	        type: "GET",
	        contentType:"application/json;charset=utf-8",
	        url: "http://127.0.0.1:8080/blog/user/getVeriCode",
	        data:{"email": email},
	        success: function (res) {
	        	var rs = JSON.parse(res);
	        	if(rs.code == 200){
	        		layer.msg("获取成功！")
	        		layui.sessionData('veriCode',{
	        			key: 'data',
	        			value: rs.data.data
	        		})
	        		//示例
	        		var curTime = new Date()
	        		, dat = new Date(curTime.setSeconds(curTime.getSeconds() + 300));
	        		var serverTime = curTime.getTime() //假设为当前服务器时间，这里采用的是本地时间，实际使用一般是取服务端的
	        		, endTime = dat.getTime();
	        		util.countdown(endTime, serverTime, function(date, serverTime, timer){
	        		    var str = date[0] + '天' + date[1] + '时' +  date[2] + '分' + date[3] + '秒';
	        		    layui.sessionData('veriCode',{
		        			key: 'data',
		        			remove: true
		        		})
	        		  });
	        	}
	        	
	        }
   		 });
	})
	//进入注册页面	
	$('#toRegister').on('click',function(){
		$('#accountBlock').addClass('layui-hide')
		$('#loginBtn').addClass('layui-hide')
		$('#registerBtn').removeClass('layui-hide')
		$('#toLogin').removeClass('layui-hide')
		$('#emailBlock').removeClass('layui-hide')
		$('#veriCodeBlock').removeClass('layui-hide')
		$('#nameBlock').removeClass('layui-hide')
	})
	//注册
	$('#register').on('click',function(){
		var email =  $('#email').val()
		,name = $('#name').val()
		,password = $('#pwd').val()
		, veriCode = $('#veriCode').val();
		if($.isEmptyObject(email) ||$.isEmptyObject(name) || $.isEmptyObject(password) || $.isEmptyObject(veriCode) ){
			layer.msg('请输入完整信息！')
			return false;
		}
	   	 if(!checkEmail(email)){
			 layer.msg('邮箱格式有误！')
			 return false;
		 }
		var data = {
			'email': email
			,'name': name
			,'password': password
		}
		console.log(data)
		$.ajax({
	        type: "POST",
	        contentType:"application/json;charset=utf-8",
	        url: "http://127.0.0.1:8080/blog/user/register",
	        data:JSON.stringify(data),
	        success: function (res) {
	        	var rs = JSON.parse(res);
	        	if(rs.code == 200){
	        		layer.msg("注册成功！")
	        		layui.sessionData('user',{
	        			key: 'data',
	        			value: rs.data.data
	        		})
	        		window.location.href="/blog/blogg/person.html"
	        	}
	        	
	        }
   		 });  
	})
	})
	layui.carousel.render({
	    elem: '#carousel'
	    ,width: '100%' //设置容器宽度
	    ,arrow: 'always' //始终显示箭头
	    //,anim: 'updown' //切换动画方式
  	});